﻿'/* Copyright (c) 2009, Graeme Grant (graeme@live.com.au)
' * All rights reserved.
' *
' * Redistribution and use in source and binary forms, with or without
' * modification, are permitted provided that the following conditions are met:
' *
' *   * Redistributions of source code must retain the above copyright
' *     notice, this list of conditions and the following disclaimer.
' * 
' *   * Redistributions in binary form must reproduce the above copyright
' *     notice, this list of conditions and the following disclaimer in the
' *     documentation and/or other materials provided with the distribution.
' * 
' *   * The name Graeme Grant may not be used to endorse or promote products
' *     derived from this software without specific prior written permission.
' *
' * THIS SOFTWARE IS PROVIDED BY GRAEME GRANT ``AS IS'' AND ANY
' * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
' * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' * DISCLAIMED. IN NO EVENT SHALL Dr. WPF BE LIABLE FOR ANY
' * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
' */

Imports System.Collections.Generic
Imports System.Xml.Serialization
Imports System.IO

<Serializable(), XmlRoot("Listings")> _
Public Class Listings(Of T) : Implements IEnumerable(Of Listing)

#Region "Constructor"

    Public Sub New()

    End Sub

    Friend Sub New(ByVal _json As String)

        Dim res As Hashtable = JSON.JsonDecode(_json)
        Dim _jsonListings As ArrayList = res("results")

        For i As Integer = 0 To _jsonListings.Count - 1
            Dim ht As Hashtable = TryCast(_jsonListings(i), Hashtable)
            If Not ht Is Nothing Then
                _listings.Add(New Listing(ht))
            End If
        Next

        _result_count = JSON.ToInteger(res("count"))
        _result_page_limit = JSON.ToInteger(CType(res("params"), Hashtable)("limit"))
        _result_page_offset = JSON.ToInteger(CType(res("params"), Hashtable)("offset"))

    End Sub

#End Region

#Region "Public Properties"

    <XmlElement("listings")> _
    Private _listings As New List(Of Listing)
    Default Public Property Item(ByVal index As Integer) As Listing
        Get
            Return _Listings(index)
        End Get
        Set(ByVal value As Listing)
            _Listings.Add(value)
        End Set
    End Property

    <XmlElement("result_count")> _
    Private _result_count As Integer
    Public Property ResultCount() As Integer
        Get
            Return _result_count
        End Get
        Set(ByVal value As Integer)
            _result_count = value
        End Set
    End Property

    <XmlElement("result_page_limit")> _
    Private _result_page_limit As Integer
    Public Property ResultPageLimit() As Integer
        Get
            Return _result_page_limit
        End Get
        Set(ByVal value As Integer)
            _result_page_limit = value
        End Set
    End Property

    <XmlElement("result_page_offset")> _
    Private _result_page_offset As Integer
    Public Property ResultPageOffset() As Integer
        Get
            Return _result_page_offset
        End Get
        Set(ByVal value As Integer)
            _result_page_offset = value
        End Set
    End Property

#End Region

#Region "Pulic Methods & Functions"

    Public Sub Clear()
        _Listings.Clear()
    End Sub

    Public Sub Add(ByVal item As Listing)
        _Listings.Add(item)
    End Sub

    Public Function Count() As Integer
        Return _Listings.Count
    End Function

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Listing) _
                                    Implements IEnumerable(Of Listing).GetEnumerator
        Return _Listings.GetEnumerator
    End Function

    Public Function GetEnumerator1() As System.Collections.IEnumerator _
                                     Implements IEnumerable.GetEnumerator
        Return _Listings.GetEnumerator
    End Function

#End Region

End Class
